'use strict';

import { LineInfo } from './JcDebugInfo'

export class CodePosition {
    public _line_info: LineInfo;
    public _bc_offset: number;
    public _pkg_aid: string;

    public constructor(pkg_aid: string, bc_offset: number, line_info) {
        this._line_info = line_info;
        this._bc_offset = bc_offset;
        this._pkg_aid = pkg_aid;
    }

    public get_line_info(): LineInfo {
        return this._line_info;
    }

    public is_valid(): boolean {
        return (this._line_info != null)
    }

    get package_aid(): string {
        return this._pkg_aid;
    }

    get package_name(): string {
        if (this._line_info)
            return this._line_info.method_info.class_info.package_info.name;
        else
            return null;
    }

    get class_name(): string {
        if (this._line_info)
            return this._line_info.method_info.class_info.name;
        else
            return null;
    }

    get method_name(): string {
        if (this._line_info)
            return this._line_info.method_info.name;
        else
            return null;
    }

    get method_descriptor(): string {
        if (this._line_info)
            return this._line_info.method_info.descriptor;
        else return null;
    }

    get source_root(): string {
        if (this._line_info)
            return this._line_info.method_info.class_info.package_info.root;
        else
            return null;
    }

    get source_file(): string {
        if (this._line_info)
            return this._line_info.method_info.class_info.source_file;
        else
            return null;
    }

    get source_line(): number {
        if (this._line_info)
            return this._line_info.source_line;
        else
            return null;
    }

    get bc_offset(): number {
        return this._bc_offset;
    }

    public get_bytecode_count_to_line_end(): number {
        if (this._line_info)
            return this._line_info.method_info.location +
                this._line_info.method_info.header_size +
                this._line_info.end_pc + 1 - this._bc_offset;
    }

    public get_offset_from_method(): number {
        if (this._line_info)
            return this._bc_offset -
                this._line_info.method_info.location;
    }

    public __equals__(pos: CodePosition): boolean {
        return (this.package_aid == pos.package_aid &&
            this.bc_offset == pos.bc_offset);
    }
}
